package com.hxh.bank.branch.spider;

import com.hxh.bank.branch.vo.BankBranchVO;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.util.StringUtils;

import java.io.IOException;

/**
 * @author huangxunhui
 * Date: Created in 2021/5/16 16:52
 * Utils: IntelliJ IDEA
 * Description: appgate.cn 爬虫
 */
@Slf4j
public class AppGateSpider extends BankBranchSpider {

    /**
     * 爬取URL
     */
    private static final String URL = "https://www.appgate.cn/branch/bankBranchDetail/";

    @Override
    protected BankBranchVO parse(String bankBranchCode) {
        try {
            log.info("appgate.cn-查询支行信息：{}", bankBranchCode);

            Document doc = Jsoup.connect(URL + bankBranchCode).get();
            Elements tr = doc.select("tr");

            String bankName = tr.get(1).select("td").get(1).text();

            if(Boolean.FALSE.equals(StringUtils.hasText(bankName))){
                return null;
            }

            String provName = tr.get(2).select("td").get(1).text();
            String cityName = tr.get(3).select("td").get(1).text();

            return BankBranchVO.builder()
                    .bankName(bankName)
                    .bankCode(bankBranchCode)
                    .provName(provName)
                    .cityName(cityName)
                    .build();

        } catch (IOException e) {
            log.error("appgate.cn-支行信息查询失败:{}, 失败原因:{}", bankBranchCode, e.getLocalizedMessage());
            return null;
        }
    }

    @Override
    protected boolean useNextSpider(BankBranchVO vo) {


        return vo == null;
    }
}
